<?php
 

class baidu_bar{
	
	function __construct(){
		
	}
	
	function save_pic($url, $path)
	{
		$ext = get_ext($url);
		$filename = md5(time().$url).'.'.$ext;
		$upload = $path.$filename;
		$path = ROOTPATH.$upload;
		
		$cookie = 'BAIDUID=D9F458D7E4DA2D84DB37BB8A161A3980:FG=1; MCITY=-138%3A; BDUSS=ROSUtRfmliMDNQc2sxeEtpZDVRWnl6T1h5bndzZ2VrU3cweFZMLWFaUEtzR3hUQVFBQUFBJCQAAAAAAAAAAAEAAAAkPWwlTEVVTkdISU5HXwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMojRVPKI0VTUV; bdshare_firstime=1397241800267; cflag=65277:1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; H_PS_PSSID=; Hm_lvt_68a18099bd17263ae1ecb55159cb0f95=1397241785,1397288876,1397399699; Hm_lpvt_68a18099bd17263ae1ecb55159cb0f95=1397399742; RESOLUTION=1440';
		$ch = curl_init();	
		$fw = fopen($path, 'wb');
		
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
		curl_setopt($ch, CURLOPT_FILE, $fw);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		//curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$res = curl_exec($ch);
		//echo $res.'<br>';
		curl_close($ch);		
		fclose($fw);
		return $upload;
	}
	
	function batch_save_pic($url, $path, $num)
	{
		$html = file_get_contents($url);

		preg_match_all('/albumData(.*)script/is', $html, $out);
		if(!isset($out[0][0])){
			echo 'get html error';
			exit();
		}
		$html_list = $out[0][0];
	
		preg_match_all('/imageUrl".*"(.*)"/isU',$html_list,$list);
		$pic = array();
		if(!is_array($list[1])){
			echo 'match img error';
			exit();
		}
        $n = 0;
		foreach($list[1] as $u){
			if($n >= $num){
                break;
            }
            $u= str_replace('\\', '', $u);
			//echo $u.'<br>';
			$pic[]= $this->save_pic($u,$path);
            $n++;
		}
		if(empty($pic)){
			echo 'save pic error';
			exit();
		}
		return $pic;
	}
}    
?>